<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/17
 * Time: 11:42
 */

namespace YMApi\Service;


/**
 * Class SeminarService
 * @package SDXApi\Service
 */
use SDXApi\Model\SeminarTemplateCategoryGoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class SeminarService
 * @package SDXApi\Service
 */
class SeminarService
{
	/**
	 * @var \Model|\Think\Model
	 */
	protected static $seminarModel;

	/**
	 * @var \Model|\Think\Model
	 */
	protected static $seminarPageModel;

	/**
	 * @var
	 */
	protected $user_id;

	/**
	 * @var
	 */
	protected $goodsInfo;

	/**
	 * @var
	 */
	protected $wid;

	/**
	 * @var GoodsRepository
	 */
	protected static $goodsRepository;

	/**
	 * @var
	 */
	protected static $SeminarBagModel;

	/**
	 * @var
	 */
	protected static $SeminarCategoryGoodsModel;

	/**
	 * @var
	 */
	protected $is_show;

	/**
	 * 注入实例
	 * SeminarService constructor.
	 * @param $user_id
	 * @param $wid
	 */
	public function __construct($user_id, $wid)
	{
		self::$seminarModel = D('Seminar');
		self::$seminarPageModel = D('SeminarPage');
		self::$goodsRepository = new GoodsRepository($user_id, $wid);
		self::$SeminarBagModel = D('SeminarTemplateBag');
		self::$SeminarCategoryGoodsModel = D('SeminarTemplateCategoryGoods');

		$this->user_id = $user_id;
		$this->wid = $wid;
	}

	/**
	 * Created by Jerry
	 * @param $name
	 * @param $value
	 */
	public function __set($name, $value)
	{
		$this->$name = $value;
	}

	/**
	 * Created by Jerry
	 * @return $this
	 */
	protected function findSeminarTemplates()
	{
		return self::$seminarModel->find_seminar_templates();
	}


	/**
	 * 查找指定专题名称下的模版并返回
	 * Created by Jerry
	 * @param $name
	 * @return bool
	 */
	public function findSeminarTemplatePageByName($name)
	{
		$template = $this->findSeminarTemplateByName($name);

		$page = $this->findSeminarPagesByTemplateId($template['id']);

		if (empty($page)) {
			return '';
		}

		return $this->checkSeminarTemplatePages($page);
	}

	/**
	 * 查找指定专题id下的模版并返回
	 * Created by Jerry
	 * @param $id
	 * @return array|string
	 */
	public function findSeminarTemplatePageById($id)
	{
		$template = $this->findSeminarTemplateById($id);
		$page = $this->findSeminarPagesByTemplateId($template['id']);

		if (empty($page)) {
			return '';
		}

		return $this->checkSeminarTemplatePages($page);
	}

	/**
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	public function getTemplateById($id)
	{
		return $this->findSeminarTemplateById($id);
	}

	/**
	 * 查找专题模版
	 * Created by Jerry
	 * @param $name
	 * @return $this
	 */
	protected function findSeminarTemplateByName($name)
	{
		return self::$seminarModel->find_seminar_template_by_name($name);
	}

	/**
	 * 查找指定id模版
	 * Created by Jerry
	 * @param $id
	 */
	protected function findSeminarTemplateById($id)
	{
		return self::$seminarModel->find_seminar_template_by_id($id, $this->is_show);
	}

	/**
	 * 查找指定专题下的模版页面
	 * Created by Jerry
	 * @param $id
	 * @return $this
	 */
	protected function findSeminarPagesByTemplateId($id)
	{
		return self::$seminarPageModel->find_seminar_template_pages_by_template_id($id);
	}

	/**
	 * 处理页面数据
	 * Created by Jerry
	 * @param $pages
	 * @return array
	 */
	protected function checkSeminarTemplatePages($pages)
	{
		$data = array();
		foreach ($pages as $key => $value) {
			$data[] = self::checkSeminarTemplatePageType($value);
		}

		return $data;
	}

	/**
	 * 处理各类型数据
	 * Created by Jerry
	 * @param $page
	 * @return bool
	 */
	static protected function checkSeminarTemplatePageType($page)
	{
		if (empty($page)) {
			return false;
		}

		$data = array();
		switch ($page['type'])
		{
			case 1:
				$data = self::checkPageFont($page);
				break;
			case 2:
				$data = self::checkPageImage($page);
				break;
			case  3:
				$data = self::checkPageGoods($page);
				break;
			case 4:
				$data = self::checkBag($page);
				break;
			case 5:
				$data = self::categoryGoods($page);
				break;

			default:
				return false;
				break;
		}

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param $page
	 * @return bool
	 */
	protected static function categoryGoods($page)
	{
		if (empty($page)) {
			return false;
		}

		$categoryInfo = self::findCategoryByStencilId($page['id']);

		$goods = self::checkGoodsCategory($categoryInfo);

		$page['goods_list'] = $goods;

		$page['type'] = 3;

		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['template_id']);

		return $page;
	}

	/**
	 * Created by Jerry
	 * @param $id
	 * @return bool
	 */
	protected static function findCategoryByStencilId($id)
	{
		if (empty($id)) {
			return false;
		}

		return self::$SeminarCategoryGoodsModel->findCategoryByTemplateId($id);
	}

	/**
	 * Created by Jerry
	 * @param $category
	 * @return array|mixed
	 */
	protected static function checkGoodsCategory($category)
	{
		$goods = self::findCategoryGoodsByCategoryId($category['category_id'], $category['goods_where']);
		$goodsList = self::$goodsRepository->formatCategoryGoodsByTemplate($goods);

		foreach ($goodsList as $key => $value) {
			$goodsList[$key]['id'] = $value['goods_id'];
		}

		if (count($goodsList) > 1) {
			$goodsList = self::checkDataSort($goodsList);
		}

		$goodsList = array_merge($goodsList);

		return $goodsList;
	}

	/**
	 * Created by Jerry
	 * @param $category_id
	 * @param $goods_where
	 * @return bool|mixed
	 */
	protected function findCategoryGoodsByCategoryId($category_id, $goods_where)
	{
		return self::$goodsRepository->findCategoryGoodsByCategoryId($category_id, $goods_where);
	}


	/**
	 * 处理红包类型
	 * Created by Jerry
	 * @param $page
	 * @return bool|array
	 */
	static protected function checkBag($page)
	{
		$bagData = self::$SeminarBagModel->get_seminar_template_bag_by_id($page['id']);

		if (empty($bagData)) {
			return false;
		}

		$page['image_start'] 	= $bagData['image_start'];
		$page['image_end'] 		= $bagData['image_end'];
		$page['bag_link'] 		= $bagData['bag_link'];
		$page['bag_id']			= $bagData['bag_id'];

		$page['bag_size']		= '750 * 900';

		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['goods_type'], $page['goods_id'], $page['template_id']);

		return $page;
	}

	/**
	 * 处理字体内容
	 * Created by Jerry
	 * @param $page
	 * @return mixed
	 */
	static protected function checkPageFont($page)
	{
		unset($page['image_url'], $page['image_link'], $page['image_size'], $page['goods_type'], $page['goods_id'], $page['template_id']);
		return $page;
	}

	/**
	 * 处理图片数据
	 * Created by Jerry
	 * @param $page
	 * @return mixed
	 */
	static protected function checkPageImage($page)
	{
		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['goods_type'], $page['goods_id'], $page['template_id']);

//		旧版地址替换
		$page['image_url'] = self::checkImageUrlReplace($page['image_url']);

		$page['image_size_data'] = self::imageTypeSize($page);

		return $page;
	}

	/**
	 * Created by Jerry
	 * @param $url
	 * @return mixed
	 */
	static protected function checkImageUrlReplace($url)
	{

		$urlPos = strpos($url, '//oss.shidexian.com');
		if ($urlPos !== false) {
			$url = str_replace('//oss.', 'https://image.', $url);
		}

		return $url;
	}

	/**
	 * Created by Jerry
	 * @param $image
	 * @return mixed
	 */
	static protected function imageTypeSize($image)
	{
		$imageSize = array(
			1	=>	'750px 360px',
			2	=>	'750px 1100px',
			3	=>	'710px 360px',
			4	=>	'750px 750px',
			5	=>	'750px 176px',
			6	=>	'750px 360px',
			7	=>	'750px 250px',
			8	=>	'750px 900px',
			9	=>	'750px 100px',
			10	=>	'750px 550px',
			11	=>	'750px 50px',
		);

		return $imageSize[$image['image_size']];
	}

	/**
	 * 处理商品数据
	 * Created by Jerry
	 * @param $page
	 * @return mixed
	 */
	static protected function checkPageGoods($page)
	{
		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['template_id']);

		$goods = unserialize($page['goods_id']);

		foreach ($goods as $key => $value) {
			$goods[$key]['goods_id'] = $value['id'];
//			unset($goods[$key]['id']);
		}


		$data = self::$goodsRepository->formatGoodsByTemplate($goods);
		$data = array_merge($data);

		if (count($data) > 1) {
			$data = self::checkDataSort($data);
		}

		$page['goods_list'] = $data;
		unset($page['goods_id']);

		return $page;
	}


	/**
	 * Created by Jerry
	 * @param $data
	 * @return mixed
	 */
	static protected function checkDataSort($data)
	{
		$sort = array();
		foreach ($data as $key => $value) {
			$sort[] = $value['sort'];
		}

		array_multisort($sort, SORT_DESC, $data);

		return $data;
	}
}